/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bugsandcritters;
import java.awt.*;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Random;

/**
 *
 * @author Robert Gibbons
 */
public class Critter extends GameObject{
    
    public double Size=0;
    public static BufferedImage CritterImage[] = new BufferedImage[4];
    public static Boolean NeedsLoading = true;
    public BufferedImage CurrentImage;
    
    public double FeedingTime=0;
    public static double CritterFeedingTime = 60;
    public static int BugsForNewCritter = 5;
    
    public ArrayList<Bug> BugsEaten = new ArrayList<Bug>();
    
    public Point TestPoint= new Point();
    
    
    
     
    @Override
    public void FindNewTarget()
    {
        Random r = new Random();
        int BestFacing = r.nextInt(4);
        
        for(int i=0; i<4 ;i++)
        {
            TestPoint.FromFacing(i, X, Y);
            if(GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this) 
                    instanceof Bug)
            {
                BestFacing = i;
                break;
            }
        }
        
        for(int i=0; i<4 ;i++)
        {
            TestPoint.FromFacing(BestFacing, X, Y);
            if((GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this) 
                    instanceof Critter))
                BestFacing = TestPoint.AddFacing(BestFacing);
        }

        TestPoint.FromFacing(BestFacing, X, Y);
        
        
        if(!(GridWorld.CheckFree(TestPoint.X, TestPoint.Y, this) 
            instanceof Critter))
            {
                TargetX=TestPoint.X;
                TargetY=TestPoint.Y;
                CurrentImage = CritterImage[BestFacing];
            }
    }
    
    
    @Override
    public void Update()
    {
        FeedingTime += DeltaTime/1000000000.0;
        if(FeedingTime > CritterFeedingTime)
        {
            Size -= DeltaTime/4000000000.0;
            if(Size<0)
                this.Destroy();
        }
        else
        {
            Boolean Move = false;
            Size += DeltaTime/4000000000.0;
            if(Size>1)
            {
                Size=1;
                Move = true;
            }
            GameObject g = GridWorld.CheckFree(X, Y, this);
            
            if(g instanceof Bug)
            {
                Bug b = (Bug)g;
                if(!(GridWorld.CheckFree(b.TargetX, b.TargetY, b) instanceof Rock))
                if(b.Size > 0.99 || BugsEaten.contains(b))
                {
                    if(!b.AttackingCritter||Size < 1)
                    {
                    Move = false;
                    b.Seeds = 0;
                    b.FeedingTime = Bug.BugFeedingTime+1;
                    this.FeedingTime = 0;
                    if(!BugsEaten.contains(b))
                        BugsEaten.add(b);

                    
                    }
                    else
                    {
                        Size-=GameManager.DeltaTime/1000000000.0;
                        FeedingTime = Critter.CritterFeedingTime+1;
                        b.AttackingCritter = false;
                    }
                }
            }
            if(g instanceof Flower)
            {
                Flower f = (Flower)g;
                {
                    Move = false;
                    f.FlowerLifeTime = -1;
                    f.Update();
                }              
            }
            
            if(Move)
            {
                if(BugsEaten.size()>BugsForNewCritter-1)
                {
                    BugsEaten.clear();
                    GridWorld.AddChild(new Critter(),FromX,FromY);
                }
                super.Update();
            }
        }
    }
    
    
    
    
    
    @Override
    public void Draw(Graphics g)
    {
        if(NeedsLoading)
        {
            try
            {
                CritterImage[0] = ImageIO.read(new File("CritterImageL.png"));
                CritterImage[1] = ImageIO.read(new File("CritterImageU.png"));
                CritterImage[2] = ImageIO.read(new File("CritterImageR.png"));
                CritterImage[3] = ImageIO.read(new File("CritterImageD.png"));
                NeedsLoading = false;
            }
            catch(Exception e)
            { 
                System.out.println("Error Loading Image: \n"
                    +e.getMessage()); 
            }
        }
        else
        {
            
            if(CurrentImage == null)
                CurrentImage = CritterImage[1];
            
            BufferedImage image = CurrentImage;
            if(image != null)
            g.drawImage(image, (int)(X- image.getWidth()/2.0*Size), 
                    (int)(Y- image.getHeight()/2.0*Size),
                    (int)(Size * image.getWidth()),
                    (int)(Size * image.getHeight()),
                    null);
            
            
        }
    }
}
